package com.jy.study;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

/**
 * @Classname CompareString
 * @Description demo_String
 * @Date 2019/8/14 14:28
 * @Created by  jiaoyong
 */
public class CompareString {
    public static void main(String[] args) {
        test();
        int i=0;
        addTwo(i++);
        System.out.println(i);
    }

    private static void addTwo(int i) {
        i+=2;
    }

    private static void test() {

        String s1 = "hello‌​world‌";
        String s2 = "helloworld";
        System.out.println(s1.equals(s2));

    }


    private static void compareContent() {

        FileInputStream is = null;
        try {

            is = new FileInputStream(new File("D:\\test.txt"));
            byte[] bytes = new byte[16];
            int nRead = 0;
            StringBuilder sb = new StringBuilder();
            while ((nRead = is.read(bytes)) != -1) {
                sb.append(new String(bytes, 0, nRead));
            }
            //从文件中读取的数据，会在文件内容打头保存BOM信息，该信息指的是文件内容以什么编码方式保存的
            //以UTF-8编码时是三个字节，分别是EF BB BF，用来标记这是一个UTF8编码的文件
            String fileStr = sb.toString();

            fileStr = deleteUTF8Bom(fileStr);

            //程序定义的字符串，此处不包含不可打印字符
            String localStr = "helloworld";
            System.out.println(fileStr);
            System.out.println(localStr);
            System.out.println(localStr.equals(fileStr));
            System.out.println("文件字符串：" + Arrays.toString(fileStr.getBytes()));
            System.out.println("本地字符串：" + Arrays.toString(localStr.getBytes()));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    private static String deleteUTF8Bom(String fileStr) {

        byte[] UTF8_BOM_Bytes = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
        byte[] bytes = fileStr.getBytes();

            if (bytes[0] == UTF8_BOM_Bytes[0]
                    && bytes[1] == UTF8_BOM_Bytes[1]
                    && bytes[2] == UTF8_BOM_Bytes[2]) {
                return new String(bytes, 3, bytes.length - 3);
            }

        return fileStr;

    }
}
